var util = require('../../utils/util')
var cartBehavior = require('../../common/cartBehavior.js')
Page({
  behaviors: [cartBehavior],
  data: {
    cateAdding: false,
  },

  //反转义方法
  htmlDecodeByRegExp(str) {
    var s = "";
    if (str.length == 0) return "";
    s = str.replace(/&amp;/g, "&");
    s = s.replace(/&lt;/g, "<");
    s = s.replace(/&gt;/g, ">");
    s = s.replace(/&nbsp;/g, " ");
    s = s.replace(/&#39;/g, "\'");
    s = s.replace(/&quot;/g, "\"");
    return s;
  },

  getGoodDetail() {
    var data = {
      goods_id: getApp().globalData.enterGoodDetialID
    }
    util.sendRequest('goods/detail', 'GET', data).then(res => {
      // console.log(res)
      if (res.data.code === 0) {
        let result = res.data.result;
        //处理轮播图Str
        let imgStr = result.goods_pic + '';
        let arr = imgStr.split(',');
        let urls = getApp().globalData.urls
        for (let i in arr) {
          arr[i] = urls + arr[i]
        }
        // console.log(arr)
        this.setData({
          item: result,
          imgArr: arr,
          htmlSnip: this.htmlDecodeByRegExp(result.goods_detail)
        })
      }
    })
  },
  previewImg(e) {
    // console.log(e.currentTarget.dataset.index)
    let index = e.currentTarget.dataset.index;
    wx.previewImage({
      current: this.data.imgArr[index],
      urls: this.data.imgArr,
    })
  },

  onLoad: function (options) {
    this.getGoodDetail();
    var app = getApp()
    this.setData({
      hoverHeight: app.globalData.windowHeight - app.globalData.navHeight,
      navHeight: app.globalData.navHeight,
      urls: app.globalData.urls
    })
    this.getCart()
  },

  onReady: function () {
    getApp().globalData.backFromDetail = true
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})